﻿using Microsoft.EntityFrameworkCore;
using PublisherManagementSystem.DAL;
using Microsoft.Extensions.DependencyInjection;
using PublisherManagementSystem.Data;

var builder = WebApplication.CreateBuilder(args);

// 配置数据库连接
builder.Services.AddDbContext<PublisherDbContext>(options =>
	options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection")));

// 添加控制器
builder.Services.AddControllersWithViews();

var app = builder.Build();

// 配置HTTP管道
if (!app.Environment.IsDevelopment())
{
	app.UseExceptionHandler("/Home/Error");
	app.UseHsts();
}

app.UseHttpsRedirection();
app.UseStaticFiles();

app.UseRouting();

app.UseAuthorization();

app.MapControllerRoute(
	name: "default",
	pattern: "{controller=Home}/{action=Index}/{id?}");

using (var scope = app.Services.CreateScope())
{
    var services = scope.ServiceProvider;
    var context = services.GetRequiredService<PublisherDbContext>();
    DataInitializer.Initialize(context);
}
app.Run();